<?php

namespace App\Console\Commands;

use App\Library\Logic\SalesRecordLogic;
use App\Library\Logic\ShopOrderLogic;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class AmountWaitCommand extends Command
{
    /**
     * 普通商品订单售后完成
     *
     * @var string
     */
    protected $signature = 'command:amountWait';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     */
    public function handle(): void
    {
        $list = ShopOrderLogic::all();
        foreach ($list as $v) {
            if ($v->order_pay_api_status === 'SUCCESS') SalesRecordLogic::addOrEditOfShopOrder($v);
            $item = DB::table('shop_order_return')->where([
                'order_id' => $v->order_id,
                'or_api_status' => 'SUCCESS',
                'or_buy_status' => 6,
            ])->first();
            if (!is_null($item)) {
                DB::table('sales_record')->where([
                    'object_type' => 'shop_order',
                    'object_id' => $v->order_id,
                    'deleted_at' => null,
                ])->update([
                    'updated_at' => now()->timestamp($item->or_api_time),
                    'deleted_at' => date('Y-m-d H:i:s', $item->or_api_time),
                ]);
            }
        }
    }
}
